package com.vmove.childtrain.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;

import com.vmove.childtrain.R;

import androidx.appcompat.widget.AppCompatTextView;

public class TypefaceTextView extends AppCompatTextView {

    public TypefaceTextView(Context context) {
        this(context, null);
    }

    public TypefaceTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TypefaceTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTypefaceTextView(context, attrs);
    }

    private void initTypefaceTextView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TypefaceTextView);
        String type = typedArray.getString(R.styleable.TypefaceTextView_typeface);
        if(null==type){
            return;
        }
        Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/"+type);
        setTypeface(typeface);
        if (typedArray != null) {
            typedArray.recycle();
        }
        typeface = null;
    }
}